package de.mafr.jsystest.utils;


/**
 * Represents a string transformation: src -> dest.
 * 
 * Think sed(1) without regular expressions. They would be easy to add though.
 */
public class Substitution {
	private final String src;
	private final String dest;

	public Substitution(String src, String dest) {
		this.src = src;
		this.dest = dest;
	}

	
	/**
	 * Replace all occurrences of src in str with dest.
	 * 
	 * If null is passed in, null is returned.
	 */
	public String substitute(String str) {
		if (str == null)
			return null;
		
		return str.replace(src, dest);
	}


	/**
	 * Static helper to apply a series of substitutions to a String.
	 */
	public static String substitute(String src, Substitution... substitutions) {
		String result = src;
		
		for (Substitution subst : substitutions) {
			result = subst.substitute(result);
		}

		return result;
	}
}
